package com.weasel.weixin.commons.service;

import com.google.common.collect.ImmutableMap;
import com.weasel.http.HttpClientSecretary;
import com.weasel.http.SimpleHttpUploadAndDownloadSecretary;
import com.weasel.http.adapter.TemplateReplaceResponseAdapter;
import com.weasel.http.handler.UploadHandler;
import com.weasel.http.helper.HttpResponseHelper;
import com.weasel.http.helper.HttpUrlHelper;
import com.weasel.weixin.commons.CommonRequest;
import com.weasel.weixin.commons.config.CommonConfig;
import com.weasel.weixin.commons.config.CommonConsts;
import com.weasel.weixin.commons.service.bean.CustomService;
import org.apache.http.client.methods.CloseableHttpResponse;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * Created by Dylan on 2015/10/16.
 */
public class CustomerServiceRequest extends CommonRequest {

    public CustomerServiceRequest(CommonConfig config) {
        super(config);
    }


    /**
     * 添加客服帐号(不包括头像，所有CustomerService的headImg可以为null)
     * @param customService 客服帐号信息,并且password应该是明文的
     * @return
     * @throws IOException
     */
    public CustomerServiceResponse add(CustomService customService) throws IOException, ExecutionException {

        return postCustomerService(customService, CommonConsts.ADD_CUSTOMER_SERVICE_URL);
    }

    /**
     * 更新客服信息
     * @param customService 客服帐号信息,并且password应该是明文的
     * @return
     * @throws IOException
     * @throws ExecutionException
     */
    public CustomerServiceResponse update(CustomService customService) throws IOException, ExecutionException {

        return postCustomerService(customService,CommonConsts.UPDATE_CUSTOMER_SERVICE_URL);
    }

    /**
     * 删除客服信息
     * @param customService 客服帐号信息,并且password应该是明文的
     * @return
     * @throws IOException
     * @throws ExecutionException
     */
    public CustomerServiceResponse delete(CustomService customService) throws IOException, ExecutionException {

        return postCustomerService(customService,CommonConsts.DELETE_CUSTOMER_SERVICE_URL);
    }

    /**
     * 上传客服头像
     * @param customerServiceAccount  客服帐号
     * @param headImage  头像文件，头像图片文件必须是jpg格式，推荐使用640*640大小的图片以达到最佳效果
     * @return
     * @throws ExecutionException
     */
    public CustomerServiceResponse uploadHeadImg(String customerServiceAccount,File headImage) throws ExecutionException {

        String url = HttpUrlHelper.withURL(CommonConsts.UPLOAD_CUSTOMER_SERVICE_HEADIMAGE).addParam("access_token",getAccessToken()).addParam("kf_account",customerServiceAccount).build();
        CustomerServiceResponse response =  SimpleHttpUploadAndDownloadSecretary.upload(url, new File[]{headImage}, new UploadHandler<CustomerServiceResponse>() {
                                                @Override
                                                public CustomerServiceResponse handler(CloseableHttpResponse response) {
                                                    try {
                                                        return HttpResponseHelper.jsonResponseToEntity(response,CustomerServiceResponse.class);
                                                    } catch (IOException e) {
                                                        e.printStackTrace();
                                                    }
                                                    return null;
                                                }
                                            });
        return response;
    }

    /**
     * 获取客服帐号列表
     * @return
     * @throws ExecutionException
     * @throws IOException
     */
    public CustomerServiceListResponse getList() throws ExecutionException, IOException {

        CustomerServiceListResponse response = HttpClientSecretary.get()
                                                                  .withURI(CommonConsts.GET_CUSTOMER_SERVICE_LIST)
                                                                  .withParams(ImmutableMap.of("access_token",getAccessToken()))
                                                                  .doItWithJson(CustomerServiceListResponse.class, new TemplateReplaceResponseAdapter() {
                                                                      @Override
                                                                      public Map<String, String> getReplaceTemplate() {
                                                                          return ImmutableMap.of("kf_nick","nickname");
                                                                      }
                                                                  });
        return response;

    }



    private CustomerServiceResponse postCustomerService(CustomService customService,String url) throws IOException, ExecutionException {

        customService.encodePassword();
        CustomerServiceResponse response = HttpClientSecretary.post()
                                                              .withURI(getBaseUrl(url))
                                                              .withParam(customService)
                                                              .doItWithJson(CustomerServiceResponse.class);

        return response;
    }
}
